package com.aorise.controller.camera;

import com.aorise.model.camera.AlarmModel;
import com.aorise.service.camera.AlarmService;
import com.aorise.utils.Page;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import com.aorise.utils.json.JsonResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:psl
 * @Desicription: 智能预警控制器类
 * @Date:Created in  2019-05-28.
 * @Modified By:
 */
@RestController
@Api(description ="智能预警信息接口")
@RequiresPermissions("ZNYJFW")
public class AlarmController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AlarmService alarmService;

    /**
     * 根据筛选条件进行智能预警信息分页查询
     * HTTP 方式：GET
     * API 路径：/api/alarm/
     * 方法名：getAlarmModels
     * 方法返回类型：String
     * 方法异常：DataAccessException Exception
     */
    @ApiOperation(value = "根据筛选条件进行智能预警信息分页查询", httpMethod = "GET", response = String.class,
            notes = "后台--智能预警--根据筛选条件进行智能预警信息分页查询")
    @RequestMapping(value = "/api/alarm/pageIndex/{pageIndex}/pageNum/{pageNum}",
            method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getAlarmModels(@ApiParam(value = "异常类型", required = false) @RequestParam(value = "exception", required = false) String exception,
                                   @ApiParam(value = "预警时间", required = false) @RequestParam(value = "alarmTime", required = false) String alarmTime,
                                   @ApiParam(value = "页索引", required = true) @PathVariable(value = "pageIndex") String pageIndex,
                                   @ApiParam(value = "页大小", required = true) @PathVariable(value = "pageNum") String pageNum) {

        try {
            logger.debug("exception" + exception);
            logger.debug("alarmTime" + alarmTime);
            logger.debug("pageIndex" + pageIndex);
            logger.debug("pageNum" + pageNum);

            Page page = new Page();
            if (!StringUtils.isBlank(pageIndex)) {
                page.setCurrentPage(Long.parseLong(pageIndex));
            }
            if (!StringUtils.isBlank(pageNum)) {
                page.setEveryPage(Long.parseLong(pageNum));
            }

            /**分页查询条件*/
            Map<String, Object> map = new HashMap<>(4);
            map.put("exception", exception);
            map.put("alarmTime", alarmTime);
            map.put("beginIndex", page.getBeginIndex());
            map.put("endinIndex", page.getEndinIndex());

            List<AlarmModel> list = alarmService.getAlarmList(map, page);
            page.setList(list);
            return new JsonResponseData(true, StatusDefineMessage.GetMessage(StatusDefine.SUCCESS),
                    StatusDefine.SUCCESS, "", page).toString();
        } catch (DataAccessException e) {
            logger.error(
                    "controller:AlarmController. function:getAlarmModels...msg: getAlarmModels DataAccessException");
            logger.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            logger.error(
                    "controller:AlarmController. function:getAlarmModels. msg:getAlarmModels Exception");
            logger.error(e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }
}
